package smetana.modules.business;

import com.google.common.base.Preconditions;
import org.apache.pivot.collections.ArrayList;
import org.apache.pivot.collections.List;
import smetana.domain.join.DocPositionReport;
import smetana.domain.join.DocReport;
import smetana.domain.join.PositionReport;
import smetana.domain.filter.PositionReportFilter;
import smetana.domain.raw.Doc;
import smetana.domain.raw.Manager;
import smetana.mapper.join.ReportMapper;
import smetana.util.Pivot;
import smetana.domain.raw.Agent;
import smetana.domain.raw.PositionGroup;
import smetana.mapper.raw.AgentMapper;
import smetana.mapper.raw.ManagerMapper;
import smetana.mapper.raw.PositionGroupMapper;

/**
 * @author dmitry.mamonov
 */
class BusinessPivotImpl extends BaseImpl implements BusinessPivot {
    @Override
    public org.apache.pivot.collections.List<DocReport> pivotListOfDocReports() {
        ReportMapper reportMapper = sql.getMapper(ReportMapper.class);
        return Pivot.fromList(reportMapper.listDocReports());
    }

    @Override
    public List<Agent> pivotListOfAgents() {
        AgentMapper agentMapper = sql.getMapper(AgentMapper.class);
        return Pivot.fromList(agentMapper.listAll());
    }

    @Override
    public List<Manager> pivotListOfManagers() {
        ManagerMapper managerMapper = sql.getMapper(ManagerMapper.class);
        return Pivot.fromList(managerMapper.listAll());
    }

    @Override
    public List<PositionReport> pivotListOfPositionReports(PositionReportFilter filter) {
        ReportMapper reportMapper = sql.getMapper(ReportMapper.class);
        return Pivot.fromList(reportMapper.listPositions(filter));
    }

    @Override
    public List<PositionGroup> pivotListOfProductGroups() {
        PositionGroupMapper mapper = sql.getMapper(PositionGroupMapper.class);
        return Pivot.fromList(mapper.listAll());
    }

    @Override
    public List<DocPositionReport> pivotListOfDocPositionReports(Doc doc) {
        if (doc==null){
            return new ArrayList<DocPositionReport>();
        } else {
            ReportMapper reportMapper = sql.getMapper(ReportMapper.class);
            return Pivot.fromList(reportMapper.listDocPositionReports(doc.getId()));
        }
    }
}
